# controllers/app_container.py
from PySide6.QtWidgets import QMainWindow, QWidget, QHBoxLayout, QStackedWidget
from core.event_bus import EventBus
from core.page_manager import PageManager
from controllers.main_function import MainFunctionWidget  # 功能主模块
from views.config_views import ConfigPage1, ConfigPage2  # 配置页

class AppContainer(QMainWindow):
    """应用全局容器，负责调控整体流程（配置→功能）"""
    def __init__(self):
        super().__init__()
        self.setWindowTitle("应用全局容器")
        self.setGeometry(100, 100, 800, 600)
        
        # 核心组件
        self.event_bus = EventBus()
        self.page_manager = PageManager(self.event_bus)
        
        # 初始化全局布局（仅包含栈容器，用于切换配置页和功能页）
        self.init_global_stack()
        
        # 启动时先进入配置流程
        self.start_config_flow()

    def init_global_stack(self):
        """全局栈：用于切换配置流程和功能主模块"""
        self.global_stack = QStackedWidget()
        self.setCentralWidget(self.global_stack)

    def start_config_flow(self):
        """启动配置流程"""
        # 初始化配置栈
        self.config_stack = QStackedWidget()
        self.global_stack.addWidget(self.config_stack)
        self.global_stack.setCurrentWidget(self.config_stack)
        
        # 推入第一个配置页（通过page_manager管理栈操作）
        self.page_manager.push_page(
            self.config_stack, 
            ConfigPage1(self.event_bus, self.page_manager, self)  # 传入AppContainer实例
        )

        # # 推入第二个配置页
        # self.page_manager.push_page(
        #     self.config_stack, 
        #     ConfigPage2(self.event_bus, self.page_manager, self)
        # )

    def enter_main_function(self):
        """配置完成，进入功能主模块"""
        # 移除配置栈
        self.global_stack.removeWidget(self.config_stack)
        self.config_stack.deleteLater()
        
        # 初始化并显示功能主模块
        self.main_function = MainFunctionWidget(self.event_bus, self.page_manager)
        self.global_stack.addWidget(self.main_function)
        self.global_stack.setCurrentWidget(self.main_function)